<template>
	<view>
		<view class="editaddress">
			<view class="content">
				<view class="row">
					<view class="nominal">收货人</view>
					<view class="input"><input placeholder="请输入收货人姓名" v-model="addressData.name" type="text" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">电话号码</view>
					<view class="input"><input placeholder="请输入收货人电话号码" v-model="addressData.phone" type="number"
							maxlength="11" /></view>
				</view>
				<view class="row">
					<view class="nominal">所在地区</view>
					<view class="input selectcity" @click="openPicker()">
						<input type="text" placeholder="请选择省市区" v-model="addressData.address" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">详细地址</view>
					<view class="input">
						<textarea style="font-size: 28upx;" v-model="addressData.moreAddres" auto-height="true"
							placeholder="输入详细地址" v-if="show == false"></textarea>
					</view>
				</view>
				<view class="row">
					<view class="nominal" style="color: 666;margin-top: 10upx;">设为默认地址</view>
					<view class="input switch">
						<switch :color="colors" style="transform:scale(0.8)" @change="switchChange"
							:checked="addressData.isdefult == 1"></switch>
					</view>
				</view>
			</view>
			<view class="save">
				<view v-show="this.id!==0" class="btn" @click="deletress()">删除地址</view>
				<view class="btn" @click="dele()">保存地址</view>
			</view>
			<!-- 省市区选择 province city area初始省市区设置 show:是否显示  @sureSelectArea：确认事件 @hideShow：隐藏事件-->
			<cc-selectCity :province="province" :city="city" :area="area" :show="show" @changeClick="changeClick"
				@sureSelectArea="onsetCity" @hideShow="onhideShow"></cc-selectCity>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				colors: '#f37b1d',
				show: false,
				province: "河南省",
				city: "郑州市",
				area: "二七区",
				addressData: {
					name: '',
					phone: '',
					address: '',
					moreAddres: '',
					isdefult: 0
				},
				isShow: true,
				id: 0
			};
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
				this.getadddetil()
			}
		},
		methods: {
			//保存地址
			dele() {
				console.log(this.addressData);
				this.postaddress()
			},
			//删除地址
			deletress() {
				this.deletaddress()
			},
			async deletaddress() {
				const res = await this.$api("/api/user/delAddr", 'post', {
					id: this.id
				}).then((res) => {
					console.log(res);
					if (res.data.code == 1) {
						uni.$u.toast(res.data.msg)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					}
				})
			},
			async getadddetil() {
				const res = await this.$api("/api/user/details", 'post', {
					id: this.id
				}).then((res) => {
					console.log(res);
					this.addressData.address = res.data.data.street
					this.addressData.name = res.data.data.ad_name
					this.addressData.phone = res.data.data.ad_tel
					this.addressData.moreAddres = res.data.data.house_number
					this.addressData.isdefult = res.data.data.ad_default
				})
			},
			async postaddress() {
				const res = this.$api("/api/user/addrEdit", 'post', {
					id: this.id,
					ad_name: this.addressData.name,
					ad_tel: this.addressData.phone,
					ad_street: this.addressData.address,
					house_number: this.addressData.moreAddres,
					longitude: "110.81",
					latitude: "32.63",
					ad_default: this.addressData.isdefult
				}).then((res) => {
					// console.log(res);
					if (res.data.code == 1) {
						uni.$u.toast(res.data.msg)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					}
				})
			},
			openPicker() {
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				});

				// this.show = true
			},

			changeClick(value, value2, value3) {
				this.province = value;
				this.city = value2;
				this.area = value3;

			},
			onhideShow() {
				this.show = false
			},
			//选中省市区
			onsetCity(e) {
				let data = e.detail.target.dataset;
				let address = data.province + data.city + data.area;
				this.show = false
				this.addressData.address = address
			},
			// 
			switchChange(e) {
				if (e.detail.value) {
					this.addressData.isdefult = 1
				} else {
					this.addressData.isdefult = 0
				}
				console.log(this.addressData.isdefult);
			}

		}
	};
</script>
<style lang="scss" scoped>
	.save {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.save view {
		display: flex;
	}

	.save .btn {
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
		width: 70%;
		height: 80upx;
		border-radius: 80upx;
		background-color: #FB774B;
		color: #fff;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin: 0px 10px;
		margin-bottom: 60px;
	}

	.save .btn .icon {
		height: 80upx;
		color: #fff;
		font-size: 30upx;
		justify-content: center;
		align-items: center;
	}

	.content {
		display: flex;
		flex-wrap: wrap;
		margin-top: 40upx;
	}

	.content view {
		display: flex;
	}

	.content .row {
		width: 92%;
		margin: 0 4%;
		border-bottom: solid 1upx #eee;
	}

	.content .row .nominal {
		width: 30%;
		height: 80upx;
		font-size: 28upx;
		font-family: Droid Sans Fallback;
		font-weight: 400;
		color: rgba(51, 51, 51, 1);
		align-items: center;
	}

	.content .row .input {
		width: 70%;
		padding: 20upx 0;
		align-items: center;
		font-size: 28upx;

	}

	.content .row .input input {
		font-size: 28upx;
		color: #333333;
	}

	.content .row .switch {
		justify-content: flex-end;
	}

	.content .row .input textarea {
		min-height: 40upx;
		line-height: 40upx;
	}

	.content .del_box {
		width: 100%;
		display: block;
		overflow: hidden;
		margin-right: 30upx;
	}

	.content .del {
		width: 240upx;
		height: 80upx;
		float: right;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #F23A3A;
		margin: 0 auto;
		margin-top: 50upx;
		border-radius: 38upx;
		background-color: rgba(255, 0, 0, 0.05);
		border-bottom: solid 1upx #eee;
	}

	.selectcity input {
		width: 90%;
	}

	.selectcity image {
		width: 40upx;
		height: 40upx;
		float: right;
	}
</style>